package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.MyHomeActivityV2;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.MyFocusEntity;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;

public class MyFocusAdapter extends BaseQuickAdapter<MyFocusEntity.DataBean, BaseViewHolder> implements LoadMoreModule {
    private Context context;

    public OnItemFocusClickListener getOnItemFocusClickListener() {
        return onItemFocusClickListener;
    }

    public void setOnItemFocusClickListener(OnItemFocusClickListener onItemFocusClickListener) {
        this.onItemFocusClickListener = onItemFocusClickListener;
    }

    private OnItemFocusClickListener onItemFocusClickListener;

    public MyFocusAdapter(Context context) {
        super(R.layout.item_my_focus);
        this.context = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, MyFocusEntity.DataBean item) {
        RoundedCornersImage rci_user_header = helper.getView(R.id.rci_user_header);
        TextView tv_name = helper.getView(R.id.tv_name);
        TextView tv_focus = helper.getView(R.id.tv_focus);
        GlideUtils.loadImage(context, item.concernedHeadImgUrl + "?x-oss-process=image/resize,m_fill,w_144,h_144", rci_user_header);
        tv_name.setText(item.concernedNickname);
        if (UserAPI.getUserId().equals(item)) {
            tv_focus.setVisibility(View.GONE);
        } else {
            tv_focus.setVisibility(View.VISIBLE);
        }
        tv_focus.setText("已关注");
        tv_focus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消关注
                if (null != onItemFocusClickListener) {
                    onItemFocusClickListener.cancelFocus(item, helper.getAdapterPosition());
                }
            }
        });

        rci_user_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MyHomeActivityV2.class);
                intent.putExtra("userId", item.concernedUserId);
                context.startActivity(intent);
            }
        });
    }

    public interface OnItemFocusClickListener {
        /**
         * 添加关注
         *
         * @param item
         */
        void addFocus(MyFocusEntity.DataBean item, int position);

        void cancelFocus(MyFocusEntity.DataBean item, int position);
    }
}
